<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      <!-- 表格 -->
      <tree-table :data="catelist" :columns="columns" :expand-type="false" :selection-type="false" show-index>
        <template #isok="{row}">
          <i class="el-icon-success" v-if="row.cat_deleted===false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 按钮 -->
        <template #operation>
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
          <el-button type="warning" size="mini" icon="el-icon-setting"></el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5,10,15]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!--添加分类对话框  -->
    <el-dialog title="添加分类" :visible.sync="addCateVisible" width="30%" @close="resetCateForm">
      <el-form ref="addCateFromRef" :model="addCateFrom" :rules="addCateFromRules" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateFrom.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader :options="parentCateList" v-model="selectKeys"
            :props="{ expandTrigger: 'hover',label:'cat_name' ,checkStrictly:true,value:'cat_id'}"
            @change="handleChange" clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addCateVisible = false">Cancel</el-button>
        <el-button type="primary" @click="saveCate">OK</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data () {
    return {
      selectKeys: [],
      parentCateList: [],
      addCateFrom: {
        cat_name: '', // 分类名称
        cat_pid: 0, // 父级分类的id parent id
        cat_level: 0// 分类的等级，共有三级：0,1,2
      },
      addCateFromRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      addCateVisible: false,
      queryInfo: {
        pagenum: 1, // 当前页码值
        pagesize: 10// 每页显示多少条数据
      },
      catelist: [],
      total: 0, // 总条数
      //
      columns: [
        {
          label: '名称分类',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ]
    }
  },
  methods: {
    saveCate () {
      this.$refs.addCateFromRef.validate(async bool => {
        if (!bool) return console.log('校验失败')
        const { data: res } = await this.$http.post('categories', this.addCateFrom)
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.$message.success('添加成功')
        this.addCateVisible = false
        this.getCateList()
      })
    },
    resetCateForm () {
      this.addCateFrom.cat_pid = 0
      this.addCateFrom.cat_level = 0
      this.addCateFrom.cat_name = ''
      this.selectKeys = []
    },
    handleChange () {
      console.log(this.selectKeys)
      if (this.selectKeys.length === 0) {
        this.addCateFrom.cat_pid = 0
        this.addCateFrom.cat_level = 0
      } else {
        this.addCateFrom.cat_level = this.selectKeys.length
        this.addCateFrom.pid = this.selectKeys[this.selectKeys.length - 1]
      }
    },
    // 获取分类等级
    async showAddCateDialog () {
      this.addCateVisible = true
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      this.parentCateList = res.data
      console.log(this.parentCateList)
    },
    // 改变页码数
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCateList()
    },
    // 改变每页展示条数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('请求失败')
      this.catelist = res.data.result
      this.total = res.data.total
      // console.log(res)
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getCateList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
